package com.thememaker.lockstyle.bean.ui;

import com.thememaker.lockstyle.LockStyleCenterLeftEditUtils;
import com.thememaker.lockstyle.bean.Var;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by jiajun.jiang on 2018/2/24.
 */
public class BaseView {

    private String layer;
    private String groupName;
    private String tag;
    private String id;

    private String name;
    private String x;
    private String y;
    private String w;
    private String h;
    private String alpha;
    private String visibility;

    private Boolean visibilityFX = true;
    private Boolean unlockMoveFX = false;
    private Boolean fitFX = false;

    @XmlAttribute(name = "x")
    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    @XmlAttribute(name = "y")
    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }

    @XmlAttribute(name = "w")
    public String getW() {
        return w;
    }

    public void setW(String w) {
        this.w = w;
    }

    @XmlAttribute(name = "h")
    public String getH() {
        return h;
    }

    public void setH(String h) {
        this.h = h;
    }

    @XmlAttribute(name = "alpha")
    public String getAlpha() {
        return alpha;
    }

    public void setAlpha(String alpha) {
        this.alpha = alpha;
    }

    @XmlAttribute(name = "visibility")
    public String getVisibility() {
        return visibility;
    }

    public void setVisibility(String visibility) {
        this.visibility = visibility;
    }

    @XmlTransient
    public String getLayer() {
        return layer;
    }

    public void setLayer(String layer) {
        this.layer = layer;
    }

    @XmlTransient
    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    @XmlTransient
    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    @XmlTransient
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlAttribute(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public BaseView() {
    }

    public BaseView(String x, String y, String w, String h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    @XmlTransient
    public Boolean getVisibilityFX() {
        return visibilityFX;
    }

    public void setVisibilityFX(Boolean visibilityFX) {
        this.visibilityFX = visibilityFX;
    }

    @XmlTransient
    public Boolean getUnlockMoveFX() {
        return unlockMoveFX;
    }

    public void setUnlockMoveFX(Boolean unlockMoveFX) {
        this.unlockMoveFX = unlockMoveFX;
    }

    @XmlTransient
    public Boolean getFitFX() {
        return fitFX;
    }

    public void setFitFX(Boolean fitFX) {
        this.fitFX = fitFX;
    }

    @Override
    public String toString() {
        Field[] fields = LockStyleCenterLeftEditUtils.getAllFields(this.getClass());
        StringBuffer stringBuffer = new StringBuffer("<");
        XmlRootElement xmlAttribute = this.getClass().getAnnotation(XmlRootElement.class);
        if (xmlAttribute != null) {
            String name = xmlAttribute.name();
            stringBuffer.append(name);
            for (Field field : fields) {
                try {
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), this.getClass());
                    Method method = pd.getReadMethod();
                    XmlAttribute fieldXml = method.getAnnotation(XmlAttribute.class);
                    if (fieldXml != null) {
                        Object getValue = method.invoke(this);
                        if (getValue != null) {
                            stringBuffer.append(" " + fieldXml.name() + "=\"" + getValue.toString() + "\"");
                        }
                    }
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            stringBuffer.append(">");
        }
        return stringBuffer.toString();
    }
}
